import "./App.css";
import { RouterProvider } from "react-router-dom";
import { router } from "@/router";
import { useUserStore } from "@/store/userStore";
import { useEffect, useState } from "react";
import { getUserInfoApi } from "./api/user";
import { Toaster } from "sonner"; // ✅ 引入 Toaster

function App() {
  const { token, setUserInfo, logout } = useUserStore();
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const checkLogin = async () => {
      if (!token) {
        setLoading(false);
        return;
      }
      try {
        const res = await getUserInfoApi();
        setUserInfo(res.data);
      } catch (e) {
        console.error("获取用户信息失败：", e);
        logout();
      } finally {
        setLoading(false);
      }
    };
    checkLogin();
  }, [token]);

  if (loading) return <div>加载中...</div>;

  return (
    <>
      {/* ✅ 全局通知容器：只需放一次，全局 toast 都能用 */}
      <Toaster richColors position="top-right" />

      {/* ✅ 路由内容 */}
      <RouterProvider router={router} />
    </>
  );
}

export default App;
